
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>玩转Angular2(12)--配置以及生成表单 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="因为项目原因又玩上了Angular2(v4.0+)，《玩转Angular2》系列用于探索一些灵活或者新的用法。本文讲述我们使用表单生成的功能，来进行表单的自定义配置和生成。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/06/25/angular2-free-12-config-and-create-form/" title="玩转Angular2(12)--配置以及生成表单" itemprop="url">玩转Angular2(12)--配置以及生成表单</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2017-06-25T01:55:53.000Z" itemprop="datePublished">2017-06-25</time>
    更新日期:<time datetime="2018-12-18T15:31:30.819Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#自定义表单作为表单控件"><span class="toc-number">1.</span> <span class="toc-text">自定义表单作为表单控件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#option-dialog"><span class="toc-number">1.1.</span> <span class="toc-text">option-dialog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hidden-when-dialog"><span class="toc-number">1.2.</span> <span class="toc-text">hidden-when-dialog</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置表单"><span class="toc-number">2.</span> <span class="toc-text">配置表单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#表单的选项和结构"><span class="toc-number">2.1.</span> <span class="toc-text">表单的选项和结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#配置表单的配置"><span class="toc-number">2.2.</span> <span class="toc-text">配置表单的配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置生成表单"><span class="toc-number">3.</span> <span class="toc-text">配置生成表单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#生成json展示"><span class="toc-number">3.1.</span> <span class="toc-text">生成json展示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#读取配置"><span class="toc-number">3.2.</span> <span class="toc-text">读取配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">4.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>因为项目原因又玩上了Angular2(v4.0+)，《玩转Angular2》系列用于探索一些灵活或者新的用法。<br>本文讲述我们使用表单生成的功能，来进行表单的自定义配置和生成。<br><a id="more"></a></p>
<h2 id="自定义表单作为表单控件"><a href="#自定义表单作为表单控件" class="headerlink" title="自定义表单作为表单控件"></a>自定义表单作为表单控件</h2><hr>
<h3 id="option-dialog"><a href="#option-dialog" class="headerlink" title="option-dialog"></a>option-dialog</h3><p>上一节<a href="https://godbasin.github.io/2017/06/24/angular2-free-11-option-dialog/">《玩转Angular2(11)–使用动态表单制作选项配置对话框》</a>我们讲了使用动态表单制作选项配置对话框，这里面我们需要将这样的组件重新作为表单的一部分，在我们的自定义表单中使用。<br>我们需要调整来使得以下方式可用：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">option-dialog</span> [<span class="attr">type</span>]=<span class="string">"optionsType(control.type)"</span> [<span class="attr">formControlName</span>]=<span class="string">"control.key"</span>&gt;</span><span class="tag">&lt;/<span class="name">option-dialog</span>&gt;</span></div></pre></td></tr></table></figure>
<p>这时候我们又需要前面的自定义input神奇了：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123;customInputAccessor&#125; <span class="keyword">from</span> <span class="string">'../../class/custom-input.class'</span>;</div><div class="line">@Component(&#123;</div><div class="line">    <span class="comment">// ...</span></div><div class="line">    providers: [customInputAccessor(OptionDialogComponent)]</div><div class="line">&#125;)</div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">OptionDialogComponent</span> <span class="title">implements</span> <span class="title">OnInit</span> </span>&#123;</div><div class="line">    @Input() type: string = <span class="string">''</span>;</div><div class="line">    optionsForm: IOption[] = [];</div><div class="line">    optionControl: ICustomControl[];</div><div class="line"></div><div class="line">    isShown: boolean = <span class="literal">false</span>;</div><div class="line"></div><div class="line">    private model: IOptions[] = []; <span class="comment">// 控件的值</span></div><div class="line">    private onChange: <span class="function">(<span class="params">_: any</span>) =&gt;</span> <span class="keyword">void</span>;</div><div class="line">    private onTouched: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">void</span>;</div><div class="line">    <span class="comment">// ...其他省略</span></div><div class="line">    ngOnInit()</div><div class="line">    isValid()</div><div class="line">    setOptions() </div><div class="line">    addControl(</div><div class="line"></div><div class="line">    saveOptions() &#123;</div><div class="line">        <span class="comment">// 将valid过滤掉</span></div><div class="line">        <span class="keyword">this</span>.model = [].concat(<span class="keyword">this</span>.optionsForm.map(<span class="function">(<span class="params">op: IOption</span>) =&gt;</span> &#123;</div><div class="line">            <span class="keyword">const</span> &#123;id, text&#125; = op;</div><div class="line">            <span class="keyword">return</span> &#123;id, text&#125;;</div><div class="line">        &#125;));</div><div class="line">        <span class="keyword">this</span>.isShown = <span class="literal">false</span>;</div><div class="line">        <span class="keyword">this</span>.onChange(<span class="keyword">this</span>.model); <span class="comment">// 触发值更新</span></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">// 其他照旧</span></div><div class="line">    onBlur() &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    writeValue(value: IOptions[]): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">if</span> (value &amp;&amp; value.length) &#123;</div><div class="line">            <span class="keyword">this</span>.model = value;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    registerOnChange(fn: <span class="function">(<span class="params">_: any</span>) =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">this</span>.onChange = fn;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    registerOnTouched(fn: <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">this</span>.onTouched = fn;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="hidden-when-dialog"><a href="#hidden-when-dialog" class="headerlink" title="hidden-when-dialog"></a>hidden-when-dialog</h3><p>我们的配置选项中，有一个是<code>hiddenWhen</code>参数，该参数也是通过配置多项条件的方式，这里我们也添加一个<code>hidden-when-dialog</code>来配置。<br>由于篇幅关系，这里只看看配置文件：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> validationsFormControl: ICustomControl[] = [</div><div class="line">    &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'text'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'key'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'key'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'key必填'</span></div><div class="line">        &#125;]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'select'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'validate'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'validate'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'validate必选'</span></div><div class="line">        &#125;],</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'&gt;'</span>, <span class="attr">text</span>: <span class="string">'&gt;'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'&gt;='</span>, <span class="attr">text</span>: <span class="string">'&gt;='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'&lt;'</span>, <span class="attr">text</span>: <span class="string">'&lt;'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'&lt;='</span>, <span class="attr">text</span>: <span class="string">'&lt;='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'=='</span>, <span class="attr">text</span>: <span class="string">'=='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'==='</span>, <span class="attr">text</span>: <span class="string">'==='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'!='</span>, <span class="attr">text</span>: <span class="string">'!='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'!=='</span>, <span class="attr">text</span>: <span class="string">'!=='</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'indexOf'</span>, <span class="attr">text</span>: <span class="string">'indexOf'</span>&#125;,</div><div class="line">        ]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'text'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'param'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'param'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'param必填'</span></div><div class="line">        &#125;]</div><div class="line">    &#125;</div><div class="line">];</div></pre></td></tr></table></figure>
<p>至于组件的html和js大家可以翻阅项目代码（在文章最后有链接）。</p>
<h2 id="配置表单"><a href="#配置表单" class="headerlink" title="配置表单"></a>配置表单</h2><hr>
<h3 id="表单的选项和结构"><a href="#表单的选项和结构" class="headerlink" title="表单的选项和结构"></a>表单的选项和结构</h3><p>目前为止，我们的表单控件格式大致是下面这些：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> interface ICustomControl &#123;</div><div class="line">    <span class="attr">type</span>: string;</div><div class="line">    label: string;</div><div class="line">    key: string;</div><div class="line">    validations?: IValidations[];</div><div class="line">    options?: IOptions[];</div><div class="line">    limit?: ILimit;</div><div class="line">    hiddenWhen?: IHiddenCondition;</div><div class="line">    description?: string;</div><div class="line">    setOptions?: boolean;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>我们需要添加一些选项或者是输入，来让使用的人生成以上的格式，然后生成想要的表单。<br>这里面需要详细配置的有：</p>
<ul>
<li><code>validations</code><ul>
<li>校验方式，如<code>required</code>、<code>maxLength</code>等</li>
<li>这里可以使用<code>checkbox-with-input</code>的方式来配置</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> interface IValidations &#123;</div><div class="line">    <span class="attr">type</span>: string;</div><div class="line">    param?: any;</div><div class="line">    message: string;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li><code>options</code><ul>
<li>选项配置，当选择的类型<code>type</code>需要配置选项时出现</li>
<li><code>type == &#39;select&#39; || type == &#39;radio&#39; || type == &#39;checkbox&#39;</code>等等</li>
<li>前面的<code>option-dialog</code>已封装该功能，并提供自定义表单使用，但目前只支持<code>id</code>和<code>text</code>的配置</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> interface IOptions &#123;</div><div class="line">    <span class="attr">id</span>: string;</div><div class="line">    text: string;</div><div class="line">    withInput?: boolean;</div><div class="line">    type?: <span class="string">'text'</span> | <span class="string">'number'</span> | <span class="string">'email'</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li><code>limit</code><ul>
<li>图片校验，只当选择图片上传时使用</li>
<li>使用<code>checkbox-with-input</code>的方式来配置</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> interface ILimit &#123;</div><div class="line">    width?: number;</div><div class="line">    height?: number;</div><div class="line">    size?: number;</div><div class="line">    type?: string;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li><code>hiddenWhen</code><ul>
<li>条件符合时隐藏，可配置多个条件，并设置多个条件的关系（<code>||</code>或<code>&amp;&amp;</code>）</li>
<li><code>hidden-when-dialog</code></li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> interface IHiddenCondition &#123;</div><div class="line">    <span class="attr">condition</span>: <span class="string">'||'</span> | <span class="string">'&amp;&amp;'</span> | <span class="string">''</span>;</div><div class="line">    validations?: IHiddenValidate[];</div><div class="line">&#125;</div><div class="line"><span class="keyword">export</span> interface IHiddenValidate &#123;</div><div class="line">    <span class="attr">key</span>: string;</div><div class="line">    validate: string;</div><div class="line">    param: string | number;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="配置表单的配置"><a href="#配置表单的配置" class="headerlink" title="配置表单的配置"></a>配置表单的配置</h3><p>接下来我们来配置生成这样一个用来生成配置的表单吧：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> normalFormControl: ICustomControl[] = [</div><div class="line">    &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'select'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'类型'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'类型必选'</span></div><div class="line">        &#125;],</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'text'</span>, <span class="attr">text</span>: <span class="string">'input-text'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'number'</span>, <span class="attr">text</span>: <span class="string">'input-数字'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'select'</span>, <span class="attr">text</span>: <span class="string">'select'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'radio'</span>, <span class="attr">text</span>: <span class="string">'radio组'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'checkbox'</span>, <span class="attr">text</span>: <span class="string">'checkbox组'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'date'</span>, <span class="attr">text</span>: <span class="string">'日期'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'date-time'</span>, <span class="attr">text</span>: <span class="string">'日期+时间(分钟)'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'date-hour'</span>, <span class="attr">text</span>: <span class="string">'日期+时间(小时)'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'upload-image'</span>, <span class="attr">text</span>: <span class="string">'图片上传'</span>&#125;,</div><div class="line">        ]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'text'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'字段key'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'key'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'字段key必填'</span></div><div class="line">        &#125;, &#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'maxLength'</span>,</div><div class="line">            <span class="attr">param</span>: <span class="number">15</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'字段key最多15个字符'</span></div><div class="line">        &#125;, &#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'minLength'</span>,</div><div class="line">            <span class="attr">param</span>: <span class="number">3</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'字段key最少3个字符'</span></div><div class="line">        &#125;]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'text'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'标签说明'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'label'</span>,</div><div class="line">        <span class="attr">validations</span>: [&#123;</div><div class="line">            <span class="attr">type</span>: <span class="string">'required'</span>,</div><div class="line">            <span class="attr">message</span>: <span class="string">'标签说明必填'</span></div><div class="line">        &#125;]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'checkbox-with-input'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'校验'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'validations'</span>,</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'required'</span>, <span class="attr">text</span>: <span class="string">'必填/必选'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'email'</span>, <span class="attr">text</span>: <span class="string">'邮件格式'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'maxLength'</span>, <span class="attr">text</span>: <span class="string">'最大字符数'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'number'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'minLength'</span>, <span class="attr">text</span>: <span class="string">'最小字符数'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'number'</span>&#125;</div><div class="line">        ]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'checkbox-with-input'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'图片上传限制'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'limit'</span>,</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'width'</span>, <span class="attr">text</span>: <span class="string">'宽(px)'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'number'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'height'</span>, <span class="attr">text</span>: <span class="string">'高(px)'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'number'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'size'</span>, <span class="attr">text</span>: <span class="string">'大小'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'number'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'type'</span>, <span class="attr">text</span>: <span class="string">'类型'</span>, <span class="attr">withInput</span>: <span class="literal">true</span>, <span class="attr">type</span>: <span class="string">'text'</span>&#125;</div><div class="line">        ],</div><div class="line">        <span class="attr">description</span>: <span class="string">'类型限制可填 jpg | png | gif'</span>,</div><div class="line">        <span class="attr">hiddenWhen</span>: &#123; <span class="comment">// 当选择上传图片组件才显示</span></div><div class="line">            condition: <span class="string">'||'</span>,</div><div class="line">            <span class="attr">validations</span>: [&#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'upload-image'</span></div><div class="line">            &#125;]</div><div class="line">        &#125;</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'select'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'选项配置'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'options'</span>,</div><div class="line">        <span class="attr">options</span>: [],</div><div class="line">        <span class="attr">hiddenWhen</span>: &#123; <span class="comment">// 选择带选项组件显示</span></div><div class="line">            condition: <span class="string">'&amp;&amp;'</span>,</div><div class="line">            <span class="attr">validations</span>: [&#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'select'</span></div><div class="line">            &#125;, &#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'checkbox'</span></div><div class="line">            &#125;, &#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'radio'</span></div><div class="line">            &#125;]</div><div class="line">        &#125;,</div><div class="line">        <span class="attr">setOptions</span>: <span class="literal">true</span></div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'text'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'描述说明'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'description'</span></div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'radio'</span>, <span class="comment">// 带选项的可配置提供自定义选项</span></div><div class="line">        label: <span class="string">'是否自定义选项'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'setOptions'</span>,</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">''</span>, <span class="attr">text</span>: <span class="string">'否'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'withOption'</span>, <span class="attr">text</span>: <span class="string">'是'</span>&#125;,</div><div class="line">        ],</div><div class="line">        <span class="attr">hiddenWhen</span>: &#123;</div><div class="line">            <span class="attr">condition</span>: <span class="string">'&amp;&amp;'</span>,</div><div class="line">            <span class="attr">validations</span>: [&#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'select'</span></div><div class="line">            &#125;, &#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'checkbox'</span></div><div class="line">            &#125;, &#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'type'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'radio'</span></div><div class="line">            &#125;]</div><div class="line">        &#125;</div><div class="line">    &#125;, &#123; </div><div class="line">        <span class="attr">type</span>: <span class="string">'radio'</span>, <span class="comment">// 该字段用来判断是否启用下面配置，不作为最终生成的配置选项</span></div><div class="line">        label: <span class="string">'是否启用条件隐藏'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'withValidation'</span>,</div><div class="line">        <span class="attr">options</span>: [</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'0'</span>, <span class="attr">text</span>: <span class="string">'否'</span>&#125;,</div><div class="line">            &#123;<span class="attr">id</span>: <span class="string">'1'</span>, <span class="attr">text</span>: <span class="string">'是'</span>&#125;,</div><div class="line">        ]</div><div class="line">    &#125;, &#123;</div><div class="line">        <span class="attr">type</span>: <span class="string">'hidden-when-dialog'</span>,</div><div class="line">        <span class="attr">label</span>: <span class="string">'隐藏条件配置'</span>,</div><div class="line">        <span class="attr">key</span>: <span class="string">'hiddenWhen'</span>,</div><div class="line">        <span class="attr">hiddenWhen</span>: &#123;</div><div class="line">            <span class="attr">condition</span>: <span class="string">'&amp;&amp;'</span>,</div><div class="line">            <span class="attr">validations</span>: [&#123;</div><div class="line">                <span class="attr">key</span>: <span class="string">'withValidation'</span>,</div><div class="line">                <span class="attr">validate</span>: <span class="string">'!='</span>,</div><div class="line">                <span class="attr">param</span>: <span class="string">'1'</span></div><div class="line">            &#125;]</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">];</div></pre></td></tr></table></figure>
<p>大家会发现，上面的配置并不完全可以得到我们最后想要的配置，如<code>limit</code>和<code>options</code>的格式，故我们最后还需要输出时调整：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line">formJson() &#123;</div><div class="line">    <span class="keyword">const</span> jsonResult = [];</div><div class="line">    <span class="keyword">this</span>.customForms.forEach(<span class="function"><span class="params">form</span> =&gt;</span> &#123;</div><div class="line">        <span class="comment">// 取出需要使用的字段</span></div><div class="line">        <span class="keyword">const</span> &#123;type, label, key, limit, validations, options, description, setOptions, withValidation, hiddenWhen&#125; = form;</div><div class="line">        <span class="keyword">const</span> validationArr = [];</div><div class="line">        <span class="keyword">const</span> limitObj = &#123;&#125;;</div><div class="line">        <span class="keyword">if</span> (validations) &#123;</div><div class="line">            <span class="built_in">Object</span>.keys(validations).forEach(<span class="function"><span class="params">valid</span> =&gt;</span> &#123;</div><div class="line">                <span class="keyword">const</span> value = validations[valid].value;</div><div class="line">                <span class="keyword">let</span> message = valid;</div><div class="line">                <span class="keyword">if</span> (value != <span class="literal">null</span>) &#123;</div><div class="line">                    message += <span class="string">`: <span class="subst">$&#123;value&#125;</span>`</span>;</div><div class="line">                &#125;</div><div class="line">                <span class="keyword">if</span> (validations[valid].checked) &#123;</div><div class="line">                    validationArr.push(&#123;</div><div class="line">                        <span class="attr">type</span>: valid,</div><div class="line">                        <span class="attr">param</span>: value == <span class="literal">null</span> ? <span class="literal">undefined</span> : <span class="built_in">Number</span>(value),</div><div class="line">                        message</div><div class="line">                    &#125;);</div><div class="line">                &#125;</div><div class="line">            &#125;);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (limit) &#123;</div><div class="line">            <span class="built_in">Object</span>.keys(limit).forEach(<span class="function"><span class="params">con</span> =&gt;</span> &#123;</div><div class="line">                <span class="keyword">if</span> (limit[con].checked) &#123;</div><div class="line">                    limitObj[con] = limit[con].value;</div><div class="line">                &#125;</div><div class="line">            &#125;);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">if</span> (withValidation != <span class="string">'1'</span>) &#123;</div><div class="line">            hiddenWhen = <span class="literal">undefined</span>;</div><div class="line">        &#125;</div><div class="line">        jsonResult.push(&#123;</div><div class="line">            <span class="attr">limit</span>: limitObj,</div><div class="line">            <span class="attr">validations</span>: validationArr,</div><div class="line">            type, label, key, options, description, setOptions, hiddenWhen</div><div class="line">        &#125;);</div><div class="line">    &#125;);</div><div class="line">    <span class="keyword">this</span>.json = <span class="built_in">JSON</span>.stringify(&#123;jsonResult&#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>而我们的html模版长这样：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">"margin-top: 30px;"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>自定义表单配置<span class="tag">&lt;/<span class="name">h1</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"btn btn-info"</span> (<span class="attr">click</span>)=<span class="string">"addControl()"</span> &gt;</span>添加表单选项<span class="tag">&lt;/<span class="name">a</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span> *<span class="attr">ngFor</span>=<span class="string">"let form of customForms; let i = index;"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>表单控件&#123;&#123;i + 1&#125;&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">dynamic-form</span> [<span class="attr">config</span>]=<span class="string">"customControl"</span> [<span class="attr">model</span>]=<span class="string">"form"</span>&gt;</span><span class="tag">&lt;/<span class="name">dynamic-form</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"btn btn-success"</span> (<span class="attr">click</span>)=<span class="string">"formJson()"</span> &gt;</span>生成JSON<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">json-to-html</span> [<span class="attr">hidden</span>]=<span class="string">"!json"</span> [(<span class="attr">ngModel</span>)]=<span class="string">"json"</span>&gt;</span><span class="tag">&lt;/<span class="name">json-to-html</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<h2 id="配置生成表单"><a href="#配置生成表单" class="headerlink" title="配置生成表单"></a>配置生成表单</h2><hr>
<h3 id="生成json展示"><a href="#生成json展示" class="headerlink" title="生成json展示"></a>生成json展示</h3><p>上面大家也看到了<code>json-to-html</code>，其实主要拿以前实现的其中一种方法来生成带样式的json，具体实现大家可以参考<a href="https://godbasin.github.io/2016/11/13/json-to-html-1-use-string-regular/">《将json输出为html(一)：字符串正则匹配》</a>和<a href="https://godbasin.github.io/2016/11/13/json-to-html-2-use-object/">《将json输出为html(二)：js数据类型判断实现》</a>。</p>
<p>这里我们将它稍微封装一下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">@Component(&#123;</div><div class="line">    <span class="attr">selector</span>: <span class="string">'json-to-html'</span>,</div><div class="line">    <span class="attr">template</span>: <span class="string">`</span></div><div class="line">        &lt;div&gt;&lt;/div&gt;`,</div><div class="line">    <span class="attr">providers</span>: [customInputAccessor(JsonToHtmlComponent)]</div><div class="line">&#125;)</div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">JsonToHtmlComponent</span> <span class="title">implements</span> <span class="title">AfterViewInit</span> </span>&#123;</div><div class="line">    @Input() options: object = &#123;&#125;;</div><div class="line">    @Input() isEdit: boolean = <span class="literal">false</span>;</div><div class="line">    @Output() change = <span class="keyword">new</span> EventEmitter();</div><div class="line"></div><div class="line">    private editor: any;</div><div class="line">    private el;</div><div class="line"></div><div class="line">    private model: any = []; <span class="comment">// 控件的值</span></div><div class="line">    private onChange: <span class="function">(<span class="params">_: any</span>) =&gt;</span> <span class="keyword">void</span>;</div><div class="line">    private onTouched: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">void</span>;</div><div class="line"></div><div class="line">    <span class="keyword">constructor</span>(el: ElementRef) &#123;</div><div class="line">        <span class="keyword">this</span>.el = el;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    ngAfterViewInit() &#123;</div><div class="line">        <span class="keyword">this</span>.setValue(<span class="keyword">this</span>.model);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    setValue(value: any)&#123;</div><div class="line">        <span class="keyword">if</span>(value)&#123;</div><div class="line">            <span class="keyword">this</span>.el.nativeElement.innerHTML = JsonToHtml(value);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    writeValue(value: string): <span class="keyword">void</span> &#123;</div><div class="line">        <span class="keyword">if</span> (value) &#123;</div><div class="line">            <span class="keyword">this</span>.model = value;</div><div class="line">            <span class="keyword">this</span>.setValue(value);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="comment">// 其他照旧</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="读取配置"><a href="#读取配置" class="headerlink" title="读取配置"></a>读取配置</h3><p>我们将上面生成的json复制一下，粘贴到一个地方，然后就可以生成想要的表单了。<br>这里配置读取很简单：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">"margin-top: 30px;"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">h1</span>&gt;</span>自定义表单生成<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span>输入json<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">textarea</span> [(<span class="attr">ngModel</span>)]=<span class="string">"json"</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">h2</span>&gt;</span><span class="tag">&lt;<span class="name">a</span> <span class="attr">class</span>=<span class="string">"btn btn-success"</span> (<span class="attr">click</span>)=<span class="string">"buildForm()"</span> &gt;</span>生成表单<span class="tag">&lt;/<span class="name">a</span>&gt;</span><span class="tag">&lt;/<span class="name">h2</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">dynamic-form</span> *<span class="attr">ngIf</span>=<span class="string">"isShown"</span> [<span class="attr">config</span>]=<span class="string">"customControl"</span>&gt;</span><span class="tag">&lt;/<span class="name">dynamic-form</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div></pre></td></tr></table></figure>
<p>使用<code>*ngIf</code>主要是方便控制<code>[config]</code>的输入更改的时候重新生成，因为自定义表单通常不会动态改变，故我们不将该功能拓展进去。<br>生成配置也很简单：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">customControl: ICustomControl[];</div><div class="line">json: any;</div><div class="line">isShown: boolean = <span class="literal">false</span>;</div><div class="line"></div><div class="line">buildForm()&#123;</div><div class="line">    <span class="keyword">this</span>.isShown = <span class="literal">false</span>;</div><div class="line">    <span class="keyword">const</span> config = <span class="built_in">JSON</span>.parse(<span class="keyword">this</span>.json);</div><div class="line">    <span class="keyword">this</span>.customControl = config.jsonResult;</div><div class="line">    <span class="keyword">this</span>.isShown = <span class="literal">true</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这样，我们就能实现表单的创建和生成啦。效果图如下：</p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674116%281%29.png" alt="image"></p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674140%281%29.png" alt="image"></p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674168%281%29.png" alt="image"></p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674345%281%29.png" alt="image"></p>
<p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496674356%281%29.png" alt="image"></p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>项目中表单配置部分大致的实现也是差不多告一段落啦，后面可能更多的是补充和优化而已。<br>而其实项目还可以多种拓展呢，像这样的添加配置像（validations和hiddenWhen）也可以通过抽象做成更加通用的功能。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-free/12-config-and-create-form" target="_blank" rel="external">此处查看项目代码</a><br><a href="http://angular2-free.godbasin.com/12-config-and-create-form/index.html#/home/page-setting" target="_blank" rel="external">此处查看页面效果</a></p>
<p>完整项目也可查看<a href="https://github.com/godbasin/angular-custom-app" target="_blank" rel="external">angular-custom-app项目</a>。</p>
  
		
		<p style="margin-top:50px;">查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a></p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/angular2火锅/">angular2火锅</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/06/30/angular2-free-13-dynamic-list/" title="玩转Angular2(13)--动态列表配置">
  <strong>PREVIOUS:</strong><br/>
  <span>
  玩转Angular2(13)--动态列表配置</span>
</a>
</div>


<div class="next">
<a href="/2017/06/24/angular2-free-11-option-dialog/"  title="玩转Angular2(11)--使用动态表单制作选项配置对话框">
 <strong>NEXT:</strong><br/> 
 <span>玩转Angular2(11)--使用动态表单制作选项配置对话框
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#自定义表单作为表单控件"><span class="toc-number">1.</span> <span class="toc-text">自定义表单作为表单控件</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#option-dialog"><span class="toc-number">1.1.</span> <span class="toc-text">option-dialog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#hidden-when-dialog"><span class="toc-number">1.2.</span> <span class="toc-text">hidden-when-dialog</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置表单"><span class="toc-number">2.</span> <span class="toc-text">配置表单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#表单的选项和结构"><span class="toc-number">2.1.</span> <span class="toc-text">表单的选项和结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#配置表单的配置"><span class="toc-number">2.2.</span> <span class="toc-text">配置表单的配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置生成表单"><span class="toc-number">3.</span> <span class="toc-text">配置生成表单</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#生成json展示"><span class="toc-number">3.1.</span> <span class="toc-text">生成json展示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#读取配置"><span class="toc-number">3.2.</span> <span class="toc-text">读取配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">4.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/11/vue-for-everyone-2/" title="9102全员学Vue--2.怎么三两下拼出一个页面">9102全员学Vue--2.怎么三两...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/30/about-front-end-2-principle/" title="前端这几年--2.工作原则和选择">前端这几年--2.工作原则和选择...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/27/vue-for-everyone-1/" title="9102全员学Vue--1.如何理解前端和Vue">9102全员学Vue--1.如何理解...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-latest-20190617/" title="小程序开发月刊第六期（20190617）">小程序开发月刊第六期（2019061...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-generate/" title="小程序的诞生">小程序的诞生</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/05/15/wxapp-latest-20190515/" title="小程序开发月刊第五期（20190515）">小程序开发月刊第五期（2019051...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/15/wxapp-latest-20190415/" title="小程序开发月刊第四期（20190415）">小程序开发月刊第四期（2019041...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/13/about-front-end-1-begin-in/" title="前端这几年--1.转岗之路">前端这几年--1.转岗之路...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/15/wxapp-latest-20190315/" title="小程序开发月刊第三期（20190315）">小程序开发月刊第三期（2019031...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/10/work-2-article/" title="写文章这件事">写文章这件事</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>28</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>21</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>65</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
